package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@RestController
public class CookieController {
    @RequestMapping("add")
    public String addCookie(HttpServletResponse response){
        Cookie c1 = new Cookie("name", "tom");
        Cookie c2 = new Cookie("UserId", "23");
        //设置过期时间
        c2.setMaxAge(8);
        //想客户端发送信息
        response.addCookie(c1);
        response.addCookie(c2);
        return "ok";
    }
    @RequestMapping("getCookie")
    public String getCookie(HttpServletRequest request){
        //得到客户端提交的cookie信息
        Cookie[] cookies = request.getCookies();
        String str="";
        for (Cookie c: cookies){
            str+="健"+c.getName()+"值"+c.getValue();
        }
        return str;
    }
}
